package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daxidi.cn.entity.Log;
import com.daxidi.cn.mapper.LogMapper;
import com.daxidi.cn.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lby
 * @since 2020-09-25
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
    @Autowired
    private LogMapper logMapper;

    @Override
    public Map selectAll(Integer pageNum, Integer limit) {
        Page<Log> page = new Page<>(pageNum, limit);
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        Page<Log> bzLogPage = logMapper.selectPage(page, null);
        List<Log> records = bzLogPage.getRecords();
        long total = bzLogPage.getTotal();
        Map map = new HashMap();
        map.put("code", 0);
        map.put("count", total);
        map.put("data", records);
        return map;
    }

    @Override
    public Map search(Integer pageNum, Integer pageSize, String name, String beginTime, String endTime) {
        Page<Log> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Log> wrapper = new QueryWrapper();
        if (null != name && !"".equals(name)){
            wrapper.like("username", name);
        }
        if (beginTime != null && endTime != null && !"".equals(beginTime) && !"".equals(endTime)) {
            wrapper.between("log_at", beginTime, endTime);
        }
        Page<Log> logPage = logMapper.selectPage(page, wrapper);
        List<Log> records = logPage.getRecords();
        long total = logPage.getTotal();
        Map map = new HashMap();
        map.put("code", 0);
        map.put("count", total);
        map.put("data", records);
        return map;
    }
}
